from bs4 import BeautifulSoup
import requests
import os, json


class ChinaJunQing(object):

    def __init__(self):
        self.url = 'http://mil.news.sina.com.cn/roll/index.d.html?cid=57918'

        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",

        }

    def send_request(self, count):

        res = requests.get(self.url, headers=self.headers, params={'page': count})

        return res.content.decode()

    def start(self, res, i):
        soup = BeautifulSoup(res, "lxml")

        soup_list = soup.select('.linkNews li a')

        new_list = []
        for soup in soup_list:
            new_list.append({
                "title": soup.get_text(),
                "url": soup.get('href')
            })

        if not os.path.exists('./china'):
            os.mkdir('./china')

        with open(f'./china/china_{i}.json', 'w', encoding='utf-8') as f:
            json_data = json.dumps(new_list, ensure_ascii=False)
            f.write(json_data)

    def run(self, count):
        for i in range(1, count + 1):
            res = self.send_request(count)
            self.start(res, i)


if __name__ == '__main__':
    count = int(input('请输入您想查询的页数：'))
    c = ChinaJunQing()
    c.run(count)
